package com.sh.common;

import com.sh.common.enums.BizCodeEnum;

import java.io.Serializable;
import java.util.Objects;

public class BaseResult<T> implements Serializable {

    public static final String SUCCESS_MSG = "操作成功!";
    public static final String FAILED_MSG = "操作失败!";
    public static final int SUCCESS = 200;
    public static final int FAIL = 500;
    private boolean success;
    private Integer code;
    private String message;
    private T data;

    public Boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public BaseResult() {
        this.code = 200;
        this.success = true;
        this.message = SUCCESS_MSG;
    }

    public BaseResult(T data) {
        this.code = 200;
        this.message = SUCCESS_MSG;
        this.success = true;
        this.data = data;
    }

    public BaseResult(int code, String msg) {
        this.code = code;
        this.message = msg;
        if (code == 200) {
            this.success = true;
        } else {
            this.success = false;
        }

    }

    public BaseResult(int code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
        if (code == 200) {
            this.success = true;
        } else {
            this.success = false;
        }

    }

    public static <T> BaseResult<T> result(BaseResult<T> result) {
        return new BaseResult(result.getCode(), result.getMessage(), result.getData());
    }

    public static <T> BaseResult<T> success() {
        return new BaseResult();
    }

    public static <T> BaseResult<T> success(T data) {
        return new BaseResult(data);
    }

    public static <T> BaseResult<T> fail(String message) {
        return new BaseResult(500, message, (Object)null);
    }

    public static <T> BaseResult<T> fail(int code, String message) {
        return new BaseResult(code, message, (Object)null);
    }

    public static <T> BaseResult<T> fail(final BizCodeEnum code) {
        return new BaseResult(code.getCode(), code.getMessage(), (Object)null);
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }

    public BaseResult<T> setCode(final Integer code) {
        this.code = code;
        return this;
    }

    public BaseResult<T> setMessage(final String message) {
        this.message = message;
        return this;
    }

    public BaseResult<T> setData(final T data) {
        this.data = data;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        BaseResult<?> that = (BaseResult<?>) o;
        return success == that.success &&
                Objects.equals(code, that.code) &&
                Objects.equals(message, that.message) &&
                Objects.equals(data, that.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(success, code, message, data);
    }
}
